package com.pptv.mtbu.common.hibernate3.support;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.HibernateException;

import com.pptv.mtbu.ottflow.dao.BaseDao;

import java.sql.SQLException;
import java.util.List;

/**
 * 主要提供对baseDao的支持
 * @author kinzhang
 *
 */
public abstract class MtbuHibernateDaoSupport
    extends HibernateDaoSupport
    implements BaseDao
{
    
    
    public List findByPage(final String hql, 
        final int offset, final int pageSize)
    {

        List list = getHibernateTemplate()
            .executeFind(new HibernateCallback()
        {
    
            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
            
                List result = session.createQuery(hql)
                    .setFirstResult(offset)
                    .setMaxResults(pageSize)
                    .list();
                return result;
            }
        });
        return list;
    }


    public List findByPage(final String hql , final Object value ,
        final int offset, final int pageSize)
    {

        List list = getHibernateTemplate()
            .executeFind(new HibernateCallback()
        {
    
            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
        
                List result = session.createQuery(hql)
            
                    .setParameter(0, value) 
                    .setFirstResult(offset)
                    .setMaxResults(pageSize)
                    .list();
                return result;
            }
        });
        return list;
    }

    public List findByPage(final String hql, final Object[] values,
        final int offset, final int pageSize)
    {

        List list = getHibernateTemplate()
            .executeFind(new HibernateCallback()
        {

            public Object doInHibernate(Session session)
                throws HibernateException, SQLException
            {
            
                Query query = session.createQuery(hql);

                for (int i = 0 ; i < values.length ; i++)
                {
                    query.setParameter( i, values[i]);
                }
                List result = query.setFirstResult(offset)
                    .setMaxResults(pageSize)
                    .list();
                return result;
            }
        });
        return list;
    }


    
}
